package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.stark.picselect.config.PhotoConfig;
import com.stark.picselect.entity.SelectMediaEntity;
import com.stark.picselect.utils.MediaLoadManager;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.SelFileAdapter;
import flc.ast.databinding.ActivityFileSelectBinding;
import flc.ast.utils.Content;
import io.reactivex.rxjava3.core.ObservableEmitter;
import stark.common.basic.event.EventStatProxy;
import stark.common.basic.utils.RxUtil;
import stark.common.basic.utils.StkPermissionHelper;

public class FileSelectActivity extends BaseAc<ActivityFileSelectBinding> {

    private List<String> listPath = new ArrayList<>();
    private SelFileAdapter selFileAdapter;
    private boolean isAllSel = false;

    @Override
    protected int onCreate() {
        return R.layout.activity_file_select;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(this, mDataBinding.event1Container);

        mDataBinding.tvFileSelectBack.setOnClickListener(v -> onBackPressed());
        mDataBinding.llAllSel.setOnClickListener(this);
        mDataBinding.tvConfirm.setOnClickListener(this);

        mDataBinding.rvFileSelectList.setLayoutManager(new GridLayoutManager(mContext, 3));
        selFileAdapter = new SelFileAdapter();
        mDataBinding.rvFileSelectList.setAdapter(selFileAdapter);
        selFileAdapter.setOnItemClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.tvConfirm://确认上传
                if (listPath.size() == 0) {
                    ToastUtils.showShort(R.string.please_import_file);
                    return;
                }
                Intent intent = new Intent();
                intent.putExtra(Content.ListPath, (Serializable) listPath);
                setResult(RESULT_OK, intent);
                finish();
                break;
            case R.id.llAllSel://全选
                listPath.clear();
                if (isAllSel) {
                    isAllSel = false;
                    mDataBinding.ivAllSel.setImageResource(R.drawable.aweiquanx);
                    for (SelectMediaEntity validDatum : selFileAdapter.getValidData()) {
                        validDatum.setChecked(false);
                    }
                } else {
                    isAllSel = true;
                    mDataBinding.ivAllSel.setImageResource(R.drawable.aquanxuan);
                    for (SelectMediaEntity validDatum : selFileAdapter.getValidData()) {
                        validDatum.setChecked(true);
                        listPath.add(validDatum.getPath());
                    }
                }
                selFileAdapter.notifyDataSetChanged();
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        if (selFileAdapter.getItem(position).isChecked()) {
            selFileAdapter.getItem(position).setChecked(false);
            listPath.remove(selFileAdapter.getItem(position).getPath());
        } else {
            selFileAdapter.getItem(position).setChecked(true);
            listPath.add(selFileAdapter.getItem(position).getPath());
        }
        selFileAdapter.notifyItemChanged(position);
    }

    @Override
    protected void initData() {
        getPermission();
    }

    //获取权限
    private void getPermission() {
        StkPermissionHelper.permission(StkPermissionHelper.Permission.ACCESS_ALL_MEDIA)
                .reqPermissionDesc(getString(R.string.get_storage_permission))
                .callback(new StkPermissionHelper.ACallback() {
                    @Override
                    public void onGranted() {
                        getData();
                    }

                    @Override
                    public void onDenied(boolean needGoSettings) {
                        mDataBinding.rvFileSelectList.setVisibility(View.GONE);
                        mDataBinding.ivFileSelectNotData.setVisibility(View.GONE);
                    }
                }).request();
    }

    //获取视频和图片数据
    private void getData() {
        showDialog(getString(R.string.get_data_ing));
        RxUtil.create(new RxUtil.Callback<List<SelectMediaEntity>>() {
            @Override
            public void doBackground(ObservableEmitter<List<SelectMediaEntity>> emitter) {
                List<SelectMediaEntity> listSys = MediaLoadManager.loadAllMediaData(mContext, PhotoConfig.Mode.ALL);
                emitter.onNext(listSys);
            }

            @Override
            public void accept(List<SelectMediaEntity> list) {
                dismissDialog();
                if (list != null && list.size() > 0) {
                    selFileAdapter.setList(list);
                    mDataBinding.rvFileSelectList.setVisibility(View.VISIBLE);
                    mDataBinding.ivFileSelectNotData.setVisibility(View.GONE);
                } else {
                    mDataBinding.rvFileSelectList.setVisibility(View.GONE);
                    mDataBinding.ivFileSelectNotData.setVisibility(View.VISIBLE);
                }
            }
        });
    }

}